package cn.edu.zjgsu.ito.controller;

import java.util.List;
import java.util.Map;

import cn.edu.zjgsu.ito.service.AdminExamService;
import cn.edu.zjgsu.ito.service.ServiceFactory;

import com.jfinal.core.Controller;
import com.jfinal.kit.JsonKit;
import com.jfinal.plugin.activerecord.Record;

public class AdminExamController extends Controller {

	AdminExamService adminExamService = ServiceFactory.createAdminExamService();
	
	public void index(){
		List<Record> examInf = testInf();
		List<Record> term = term();//获取学期
		List<Record> teacher = teacher();//获取教师
		List<Record> classroom = classroom();//获取教室
		List<Record> college = college();//获取教室
		List<Record> classroomType = classroomType();//获取教室类型
		
		List<Record> course = course();
		setAttr("examInf",examInf);
		setAttr("term",term);
		setAttr("teacher",teacher);
		setAttr("classroom",classroom);
		setAttr("classroomType",classroomType);
		setAttr("course",course);
		setAttr("college",college);
		render("examManage.html");
	}
	
	//获取学院
	private List<Record> college() {
		List<Record> college = adminExamService.college();
		return college;
	}

	private List<Record> course() {
		List<Record> course = adminExamService.course();
		return course;
	}

	//获取学期
	public List<Record> term(){
		List<Record> term = adminExamService.term();
		return term;
		
	}
	
	//获取所有教师
	public List<Record> teacher(){
		List<Record> teacher = adminExamService.teacher();
		return teacher;
		
	} 
	
	//获取所有教室
	public List<Record> classroom(){
		List<Record> classroom = adminExamService.classroom();
		return classroom;
		
	}
	
	//获取所有教室类型
		public List<Record> classroomType(){
			List<Record> classroomType = adminExamService.classroomType();
			return classroomType;
			
		}
		
		//获取所有教室ajax---------
		public void lookClassroom(){
			String classroomType = getPara("classroomType");
			List<Record> classroom = adminExamService.classroomAjax(classroomType);
			renderJson(JsonKit.toJson(classroom));
			
		}
		
		//获取所有教室类型
			public void lookClassroomType(){
				List<Record> classroomType = adminExamService.classroomType();
				JsonKit.toJson(classroomType);
			}
	
	//获取考试信息
	public List<Record> testInf(){
		List<Record> examInf = adminExamService.index();
		return examInf;
	}
	
	//按学期搜索考试信息
	public void termSelect(){
		String term = getPara("term");
		String courseNo = getPara("courseNo");
		List<Record> examTemp = adminExamService.termSelect(term,courseNo);
		String examInf = JsonKit.toJson(examTemp);
		renderJson(examInf);
	}
	
	//设置,修改考试信息
	public void setTestInf(){
		String testNo = getPara("testNo");
		String tNo = getPara("tNo");
		String classroom = getPara("classroom");
		String time = getPara("time");
		System.out.println("设置考试信息");
		System.out.println("testNo:" + testNo + "," + "tNo" + tNo + "," + classroom + time);
		adminExamService.setTestInf(testNo,tNo,classroom,time);
		renderJson(1);
	}
	
	
	//创建补考
	public void addTest(){
		String uuid;
        uuid = getPara("uuid");
        System.out.println(uuid+"=====");
        String temptt = "";
		int res = adminExamService.addTest(uuid);//生成一门考试
		System.out.println("返回json：" + res);
		if(res == 0){
			temptt = "[{\"" + "temp\":\"生成补考成功。\"}]";
//			"[{"+"\""+"stu"+"\""+":"+"\""+students+"\""+","+"\""+"passportNo"+"\""+":"+"\""+stud+"\"}] ";
			renderJson(temptt);
		}else if(res == 1){
			temptt = "[{\"" + "temp\":\"生成补考失败。\"}]";
			renderJson("temptt");
		}
	}
	
}
